package com.agentai.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.agentai.framework.security.user.UserDetail;
import com.agentai.system.enums.AreaScopeEnum;
import com.agentai.system.enums.DataScopeEnum;
import com.agentai.system.enums.UserStatusEnum;
import com.agentai.system.mapper.SysRoleAreaScopeMapper;
import com.agentai.system.mapper.SysRoleDataScopeMapper;
import com.agentai.system.mapper.SysRoleMapper;
import com.agentai.system.service.SysMenuService;
import com.agentai.system.service.SysOrgService;
import com.agentai.system.service.SysUserDetailsService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 用户 UserDetails 信息
 */
@Service
@AllArgsConstructor
public class SysUserDetailsServiceImpl implements SysUserDetailsService {
    private final SysMenuService sysMenuService;
    private final SysOrgService sysOrgService;
    private final SysRoleMapper sysRoleMapper;
    private final SysRoleDataScopeMapper sysRoleDataScopeMapper;
    private final SysRoleAreaScopeMapper sysRoleAreaScopeMapper;

    @Override
    public UserDetail getUserDetails(UserDetail userDetail) {
        // 账号不可用
        if (userDetail.getStatus() == UserStatusEnum.DISABLE.getValue()) {
            userDetail.setEnabled(false);
        }

        // 数据权限范围
        List<Long> dataScopeList = getDataScope(userDetail);
        userDetail.setDataScopeList(dataScopeList);

        // 区域权限范围
        List<String> areaScopeList = getAreaScope(userDetail);
        userDetail.setAreaScopeList(areaScopeList);

        // 用户权限列表
        Set<String> authoritySet = sysMenuService.getUserAuthority(userDetail);

        // 用户角色编码列表
        List<String> roleCodeList = sysRoleMapper.geRoleCodeByUserId(userDetail.getId());
        roleCodeList.forEach(roleCode -> authoritySet.add("ROLE_" + roleCode));

        userDetail.setAuthoritySet(authoritySet);

        return userDetail;
    }

    private List<Long> getDataScope(UserDetail userDetail) {
        Integer dataScope = sysRoleMapper.getDataScopeByUserId(userDetail.getId());
        if (dataScope == null) {
            return new ArrayList<>();
        }

        if (dataScope.equals(DataScopeEnum.ALL.getValue())) {
            // 全部数据权限，则返回null
            return null;
        } else if (dataScope.equals(DataScopeEnum.ORG_AND_CHILD.getValue())) {
            // 本机构及子机构数据
            List<Long> dataScopeList = sysOrgService.getSubOrgIdList(userDetail.getOrgId());
            // 自定义数据权限范围
            dataScopeList.addAll(sysRoleDataScopeMapper.getDataScopeList(userDetail.getId()));

            return dataScopeList;
        } else if (dataScope.equals(DataScopeEnum.ORG_ONLY.getValue())) {
            // 本机构数据
            List<Long> dataScopeList = new ArrayList<>();
            dataScopeList.add(userDetail.getOrgId());
            // 自定义数据权限范围
            dataScopeList.addAll(sysRoleDataScopeMapper.getDataScopeList(userDetail.getId()));

            return dataScopeList;
        } else if (dataScope.equals(DataScopeEnum.CUSTOM.getValue())) {
            // 自定义数据权限范围
            return sysRoleDataScopeMapper.getDataScopeList(userDetail.getId());
        }

        return new ArrayList<>();
    }

    private List<String> getAreaScope(UserDetail userDetail) {
        Integer areaScope = sysRoleMapper.getAreaScopeByUserId(userDetail.getId());
        userDetail.setAreaScope(areaScope);
        if (null != areaScope) {
            if (areaScope.equals(AreaScopeEnum.ALL.getValue())) {
                // 全部区域权限，则返回null
                return null;
            } else if (areaScope.equals(AreaScopeEnum.ORG_AND_CHILD.getValue()) || areaScope.equals(AreaScopeEnum.ORG_ONLY.getValue())) {
                // 本区域及子区域数据 或 本区域数据
                List<String> dataScopeList = new ArrayList<>();
                if (StrUtil.isNotBlank(userDetail.getAreaCode())) {
                    dataScopeList.add(userDetail.getAreaCode());
                }
                return dataScopeList;
            } else if (areaScope.equals(AreaScopeEnum.CUSTOM.getValue())) {
                // 自定义数据权限范围
                return sysRoleAreaScopeMapper.getAreaScopeList(userDetail.getId());
            }
        }
        return new ArrayList<>();
    }
}
